package com.gitee.liuzhi.framework.starter.utils;

import com.gitee.liuzhi.framework.starter.response.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Objects;

/**
 * Created by 2022/5/6/0006
 *
 * @author LiuZhi
 * @version V1.0
 * @description: 判断异常统一处理
 */
public class AssertUtils {

    /**
     * 条件为 true 则抛出业务异常
     */
    public static void isTrue(Boolean flag, String message) {
        if (flag) {
            assertException(message);
        }
    }

    /**
     * 条件为 false 则抛出业务异常
     */
    public static void isFalse(Boolean flag, String message) {
        if (!flag) {
            assertException(message);
        }
    }

    /**
     * 条件为 null 则抛出业务异常
     */
    public static void isNull(Object obj, String message) {
        if (Objects.isNull(obj) || StringUtils.isBlank(obj.toString().trim())) {
            assertException(message);
        }
    }

    /**
     * 集合为空, 就抛异常
     */
    public static <T> void isEmpty(Collection<T> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            assertException(message);
        }
    }

    /**
     * 集合不为空, 就抛异常
     */
    public static <T> void isNotEmpty(Collection<T> collection, String message) {
        if (CollectionUtils.isNotEmpty(collection)) {
            assertException(message);
        }
    }


    /**
     * 无条件抛出业务异常
     */
    public static void assertException(String msg) {
        throw new BusinessException(msg);
    }

}
